package com.learn.controller;

import com.learn.anno.Log;
import com.learn.pojo.Emp;
import com.learn.pojo.PageBean;
import com.learn.pojo.Result;
import com.learn.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    EmpService empService;
//    @GetMapping("/emps")
//    public Result selectByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer pageSize){
////        if(index == null || index < 1){
////            index = 1;
////        }
////        if(size == null || size < 5){
////            size = 5;
////        }
//        PageBean pb = empService.selectByPage(page,pageSize);
//        return Result.success(pb);
//    }

    @GetMapping
    public Result selectByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5")
                            Integer pageSize, String name, Short gender,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                            @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        //        if(index == null || index < 1){
        //            index = 1;
        //        }
        //        if(size == null || size < 5){
        //            size = 5;
        //        }
        PageBean pb = empService.selectByPageAndCondition(page,pageSize,name,gender,begin,end);
        return Result.success(pb);
    }

    @Log
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        log.info("批量删除员工，ids:{}", ids);
        empService.deleteByIds(ids);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result add(@RequestBody Emp emp){
        log.info("新增员工，员工数据：{}", emp);
        empService.add(emp);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getDataById(@PathVariable Integer id){
        log.info("查询的部门id为{}",id);
        Emp emp = empService.getDataById(id);
        return Result.success(emp);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工，员工数据：{}", emp);
        empService.update(emp);
        return Result.success();
    }
}
